package com.wwzh.interview.study.thread;

import java.util.concurrent.locks.Condition;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

class ShareData{ // 资源类
    private int number = 0;
    private Lock lock = new ReentrantLock();
    private Condition condition = lock.newCondition();
    public void increment() throws Exception{
        lock.lock();
        try {
            // 1 判断
            while (number != 0){
                // 等待，不能生产
                condition.await();
            }
            number++; //2 干活
            System.out.println(Thread.currentThread().getName() + number);
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }

    public void decrement() throws Exception{
        lock.lock();
        try {
            // 1 判断
            while (number == 0){
                // 等待，不能生产
                condition.await();
            }
            number--; //2 干活
            System.out.println(Thread.currentThread().getName() + number);
            condition.signalAll();
        } catch (InterruptedException e) {
            e.printStackTrace();
        } finally {
            lock.unlock();
        }
    }
}

public class ProdConsumer_TraditionDemo {
    public static void main(String[] args) {
        ShareData shareData = new ShareData();
        new Thread(()->{
            for (int i = 1; i < 5; i++) {
                try {
                    shareData.increment();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        },"AA").start();

        new Thread(()->{
            for (int i = 1; i < 5; i++) {
                try {
                    shareData.decrement();
                } catch (Exception e){
                    e.printStackTrace();
                }
            }
        },"BB").start();
    }
}
